package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.OtaAccessConfig;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.OtaAccessKeyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ota访问配置
 *
 * @author yee_
 * @version 1.0
 * @date 2023/3/8 10:47
 */
@RestController
@RequestMapping("/ota/config")
public class OtaAccessKeyController {

    @Resource
    private OtaAccessKeyService otaAccessKeyService;

    /**
     * ota 访问配置分页查询
     *
     * @param page 页码
     * @param size 每页条数
     * @return 。
     */
    @GetMapping("/listPage")
    public Result<PageResult<OtaAccessConfig>> listPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                        @RequestParam(required = false, defaultValue = "10") Integer size) {
        PageInfo<OtaAccessConfig> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(otaAccessKeyService::list);
        return Result.success(new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList()));
    }

    /**
     * 新增配置
     *
     * @param accessConfig 。
     * @return 。
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.OTAACCESSKEY, methods = MethodEnum.OTAACCESSKEY_ADD)
    public Result<String> add(@RequestBody @Validated OtaAccessConfig accessConfig) {
        checkLength(accessConfig);
        otaAccessKeyService.save(accessConfig);
        return Result.success();
    }


    /**
     * 修改配置
     *
     * @param accessConfig 。
     * @return 。
     */
    @PostMapping("/update")
    @OperateLog(module = ModuleEnum.OTAACCESSKEY, methods = MethodEnum.OTAACCESSKEY_UPDATE)
    public Result<String> update(@RequestBody @Validated OtaAccessConfig accessConfig) {
        checkLength(accessConfig);
        otaAccessKeyService.updateById(accessConfig);
        return Result.success();
    }


    /**
     * 根据品牌id查询配置
     *
     * @param brandId 品牌id
     * @return 。
     */
    @GetMapping("/findByBrandId/{brandId}")
    public Result<OtaAccessConfig> findByBrandId(@PathVariable Integer brandId) {
        OtaAccessConfig config = otaAccessKeyService.getById(brandId);
        return Result.success(config);
    }

    /**
     * 删除配置
     *
     * @param brandId 品牌id
     * @return 。
     */
    @DeleteMapping("/delete/{brandId}")
    @OperateLog(module = ModuleEnum.OTAACCESSKEY, methods = MethodEnum.OTAACCESSKEY_DEL)
    public Result<String> deleteById(@PathVariable Integer brandId) {
        otaAccessKeyService.removeById(brandId);
        return Result.success();
    }

    void checkLength(OtaAccessConfig accessConfig){
        if (StrUtil.isNotEmpty(accessConfig.getAccessKey()) && accessConfig.getAccessKey().length() > 255){
            throw new BaseException("AppKey过长");
        }else if (StrUtil.isNotEmpty(accessConfig.getAccessSecret()) && accessConfig.getAccessSecret().length() > 255){
            throw new BaseException("AppSecret过长");
        }
    }
}

